const express = require("express")
const app = express();
const path = require("path");

//处理body请求体的中间件
app.use(express.urlencoded())
app.use(express.json())

//查询数据接口
app.get("/user",(req,res)=>{
    console.log(req.headers);
    //判断请求头有没有token
    const token = req.headers.token;
    console.log(token, "token");
    //没有token
    if (!token) {
        return res.send({
        code: 10002,
        msg: "没有权限",
        data: null
        })
    }

    //有token就继续执行下面代码
    const {
        userId
    } = req.query

    if(userId !== "001"){
        return res.send({
            code: 10001,
            msg: "查询失败",
            data: null
          })
    }

    res.send({
        code: 10000,
        msg: "查询成功",
        data: {
          name: "韩立",
          age: 14300,
          sex: "男"
        }
    })
})

//定义一个延迟响应接口处理取消请求
app.get("/userLazy",(req,res)=>{
    setTimeout(()=>{
        const{
            userId
        } = req.query

        if(userId !== "001"){
            return res.send({
                code: 10001,
                msg: "查询失败",
                data: null
              })
        }
    
        res.send({
            code: 10000,
            msg: "查询成功",
            data: {
              name: "韩立",
              age: 14300,
              sex: "男"
            }
        })

    },3000)
})


//添加数据接口
app.post("/adduser",(req,res)=>{
    const {
        name,
        age,
        sex
    } = req.body

    if(!name || !age || !sex){
        return res.send({
            code: 10001,
            mes: "添加失败",
            data: null
        })
    }

    res.send({
        code: 10000,
        mes: "添加成功",
        data: null
    })
})

//修改数据接口
app.put("/updateuser",(req,res)=>{
    const{
        name,
        age,
        Lv,
    } = req.body

    res.send({
        code: 10000,
        msg: "修改成功",
        name: name,
        age: age,
        Lv: Lv
    })
})

//删除数据接口
app.delete("/deleteuser",(req,res)=>{
    const{
        userId
    } = req.query

    res.send({
        code: 10000,
        msg: "删除成功",
        userId: userId
    })
})

//静态资源接口部署

//axios基础使用
app.get("/1", (req, res) => {
    const filePath = path.resolve(__dirname, "./01.axios基础使用.html");
    res.sendFile(filePath);
})

//axios请求别名
app.get("/2", (req, res) => {
    const filePath = path.resolve(__dirname, "./02.axios请求别名.html");
    res.sendFile(filePath);
})

//创建axios实例
app.get("/3", (req, res) => {
    const filePath = path.resolve(__dirname, "./03.创建axios实例.html");
    res.sendFile(filePath);
})

//请求拦截器
app.get("/4", (req, res) => {
    const filePath = path.resolve(__dirname, "./04.请求拦截器.html");
    res.sendFile(filePath);
})

//响应拦截器
app.get("/5", (req, res) => {
    const filePath = path.resolve(__dirname, "./05.响应拦截器.html");
    res.sendFile(filePath);
})

//拦截器练习
app.get("/6", (req, res) => {
    const filePath = path.resolve(__dirname, "./06.拦截器练习.html");
    res.sendFile(filePath);
})

//取消请求
app.get("/7", (req, res) => {
    const filePath = path.resolve(__dirname, "./07.取消请求.html");
    res.sendFile(filePath);
})


app.listen(3000, () => {
    console.log("服务器请求成功 请访问 http://127.0.0.1:3000");
})